<?php
logfile('--记录时间：'.date('Y-m-d H:i:s').'------','/var/www/html/coop/callback/recode_log.txt');
if (!class_exists("go_sql")){
include("/var/www/html/new/conf/le.je");
  $le=new go_sql();
  $le->connect();
}

$ac1='16ucom';
$pw1='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac1|pw_16u=$pw1|auth_16u=true"));

$str="select orderid from pft_coop_qunar_record where status=0 and dtype=9";
$GLOBALS['le']->query($str);
$arr_orders=array();
while($GLOBALS['le']->fetch_assoc()){
	$arr_orders[]=$GLOBALS['le']->f('orderid');
}
// print_r($arr_orders);
// exit;
//foreach ($arr_orders as $v) {
	//$v就是订单号
	$v='892928';
	$search_sms=$inSide->Order_Globle_Search("", "", "", "", "", "", "", "", "", "", "", "",$v, "", "", "", "", "", "", "1", "0", "15", "0", "0", "", "0", "", "0", "", "", "", "0", "0", "0");
	$search_sms_xml= simpleXML_load_string($search_sms);
	//print_r($search_sms_xml);
	//exit;
	$ordernum=(string)$search_sms_xml->Rec->UUordernum;
	$code = (string)$search_sms_xml->Rec->UUcode;
	$aid = (string)$search_sms_xml->Rec->UUaid;
	$select = "select supplierIdentity,signkey from uu_qunar_use where fid ='$aid' limit 1";
	$GLOBALS['le']->query($select);
	$GLOBALS['le']->fetch_assoc();
	$supplierIdentity = $GLOBALS['le']->f('supplierIdentity');
	$signkey = trim($GLOBALS['le']->f('signkey'));
	$nowdate=date("Y-m-d H:i:s");
	
	$string_sms =<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request xsi:schemaLocation="http://piao.qunar.com/2013/QMenpiaoRequestSchema QMRequestDataSchema-2.0.1.xsd" xmlns="http://piao.qunar.com/2013/QMenpiaoRequestSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>Qunar.Menpiao.Agent</application>
		<processor>SupplierDataExchangeProcessor</processor>
		<version>v2.0.1</version>
		<bodyType>NoticeOrderEticketSendedRequestBody</bodyType>
		<createUser>SupplierSystemName</createUser>
		<createTime>$nowdate</createTime>
		<supplierIdentity>$supplierIdentity</supplierIdentity>
	</header>
	<body xsi:type="NoticeOrderEticketSendedRequestBody">
		<orderInfo>
			<partnerorderId>$v</partnerorderId>
			<eticketNo>$code</eticketNo>
			<eticketSended>TRUE</eticketSended>
		</orderInfo>
	</body>
</request>
XML;
	$post_string_sms= singed($string_sms,$signkey);
	
	$result = qunar_post_sms('noticeOrderEticketSended',$post_string_sms);
	$result_date = json_decode($result,true);
	$result_date_xml = simplexml_load_string(base64_decode($result_date['data']));  
	//print_r($result_date_xml);
	//exit;
	echo $qunar_code = (int)$result_date_xml->header->code;
	logfile($v."|".$qunar_code,'/var/www/html/coop/callback/recode_log.txt');
	if($qunar_code == 1000 || $qunar_code == 13002){ //1000是成功推送的  13002是没有这笔订单无须再推送
		// 推送成功需要更新pft_coop_qunar_record
		$str="update pft_coop_qunar_record set status=1 where orderid='$v' limit 1";
		$GLOBALS['le']->query($str);
	}
//}
function qunar_post_sms($aq, $cq){
	$post_data = array(); 
	$post_data['method'] = $aq; 
	$post_data['requestParam'] = $cq;
	//$url='http://agent.piao.qunar.com/api/external/supplierServiceV2.qunar';
	$url='piaoagentdev.qunar.com/api/external/supplierServiceV2.qunar';
	$o=""; 
	foreach ($post_data as $k=>$v){ 
	$o.= "$k=".urlencode($v)."&"; 
	} 
	$post_data=substr($o,0,-1); 
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1); 
	curl_setopt($ch, CURLOPT_HEADER, 0); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL,$url); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
	$reponse = curl_exec($ch);
	curl_close($ch);
	return $reponse;
}
function singed($string,$signkey){ //这里是加密
		$bstr=base64_encode($string);
		$signed=strtoupper(md5($signkey.$bstr));
		$arr=array('data'=>$bstr,'signed'=>$signed,'securityType'=>'MD5');
		
		return json_encode($arr);
}		
function logfile($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp); 
}
?>